package com.zyd.task;

import com.zyd.io.model.StationEntity;
import com.zyd.mappers.StationEntityMapper;
import org.joda.time.Interval;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
@EnableScheduling
public class StatusTask {

    private static final Logger LOGGER = LoggerFactory.getLogger(StatusTask.class);

    @Autowired
    private StationEntityMapper mapper;

    @Scheduled(fixedDelay = 300000)
    public void checkStationStatus() throws ParseException {
        List<StationEntity> stationEntities = mapper.selectByExample(null);
        for (StationEntity stationEntity : stationEntities) {
            String regtime = stationEntity.getRegtime();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date regTime = df.parse(regtime);
            Interval interval = new Interval(regTime.getTime(), new Date().getTime());
            Period period = interval.toPeriod(PeriodType.minutes());
            if (period.getMinutes() > 5) {
                LOGGER.debug("基站状态不正常,基站ID:" + stationEntity.getStationcode());
                stationEntity.setStationstatus("不正常");
                mapper.updateByPrimaryKeySelective(stationEntity);
            }
        }
        LOGGER.warn("基站状态检查结束");
    }


}
